﻿@inherits BaseComponent

@if (Model != null && Model.HasItem)
{
    var items = Model.Items.Where(i => i.Visible).ToList();
    <div class="ant-toolbar">
        @if (items.Count > Model.ShowCount)
        {
            for (int i = 0; i < Model.ShowCount - 1; i++)
            {
                var item = items[i];
                var itemName = Language.GetString(item);
                if (item.Children != null && item.Children.Count > 0)
                {
                    <AntDropdown Context="Context" TextButton="@itemName" Items="item.Children" OnItemClick="Model.OnItemClick" />
                }
                else
                {
                    <Button Disabled="@(!item.Enabled)" Danger="@(item.Style=="danger")" Type="@ButtonType.Primary" Icon="@item.Icon" OnClick="e=>Model.OnItemClick?.Invoke(item)">@itemName</Button>
                }
            }
            var mores = items.Skip(Model.ShowCount - 1).ToList();
            <AntDropdown Context="Context" TextButton="@Language["Button.More"]" Items="mores" OnItemClick="Model.OnItemClick" />
        }
        else
        {
            foreach (var item in items)
            {
                var itemName = Language.GetString(item);
                if (item.Children != null && item.Children.Count > 0)
                {
                    <AntDropdown Context="Context" TextButton="@itemName" Items="item.Children" OnItemClick="Model.OnItemClick" />
                }
                else
                {
                    <Button Disabled="@(!item.Enabled)" Danger="@(item.Style=="danger")" Type="@ButtonType.Primary" Icon="@item.Icon" OnClick="e=>Model.OnItemClick?.Invoke(item)">@itemName</Button>
                }
            }
        }
    </div>
}

@code {
    [Parameter] public ToolbarModel Model { get; set; }

    protected override void OnInitialized()
    {
        if (Model == null)
            return;

        Model.OnRefresh = StateChanged;
        base.OnInitialized();
    }
}